<?php

namespace App\Models;

class News extends Base
{

    protected $table = 'news';
    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--warning">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--danger">未通过</span>',
    ];

    const HOT_1 = 1;
    const HOT_2 = 2;
    const HOT_TEXT = [
        self::HOT_1 => '<span class="el-tag">正常</span>',
        self::HOT_2 => '<span class="el-tag el-tag--danger">热门</span>',
    ];

    const POSITION_1 = 1;
    const POSITION_2 = 2;
    const POSITION_TEXT = [
        self::POSITION_1 => '<span class="el-tag">正常</span>',
        self::POSITION_2 => '<span class="el-tag el-tag--danger">推荐</span>',
    ];
    protected $fillable = [
        'id','thumb','title','author_id','catepid','cateid','summary','status','content','state','tags','hot',
        'position','view','praise','created_at','updated_at'
    ];
    protected $casts = ['tags'=>'array'];

    public function cate(){
        return $this->belongsTo('App\Models\Cates', 'catepid', 'id');
    }

    public function cateChild(){
        return $this->belongsTo('App\Models\Cates', 'cateid', 'id');
    }

    public function user(){
        return $this->belongsTo('App\Models\Users', 'author_id', 'id');
    }


    /**
     * 前台和会员中心文章列表
     * @param $fields 查询的字段
     * @param $params 条件参数
     * @param int $limit 限制条数
     * @return mixed
     */
    protected function home($fields = ['*'], $params = [], $limit = 10){
        $data = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['author_id']) && $params['author_id']){
                    $query ->where('author_id',$params['author_id']);
                }
                if(isset($params['title']) && $params['title']){
                    $query ->where('title', 'like', '%'.$params['title'].'%');
                }
                if(isset($params['status']) && $params['status']){
                    $query ->where('status', $params['status']);
                }
                if(isset($params['position']) && $params['position']){
                    $query ->where('position',News::POSITION_2);
                }else{
                    if(isset($params['catepid']) && $params['catepid']) $query ->where('catepid',$params['catepid']);
                    if(isset($params['cateid']) && $params['cateid']) $query ->where('cateid',$params['cateid']);
                }
            })->with(['cateChild' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['cate' => function ($query) {
                $query->select(['id','name'])->get();
            }])
            ->with(['user' => function ($query) {
                $query->select(['id','truename','avatar'])->get();
            }])->orderBy('created_at','desc')
            ->paginate($limit)
            ->toArray();
        return $data;
    }

    /**
     * 根据主键ID查询一条数据
     * @param $id 主键ID
     * @param array $fields 查询字段
     * @param array $params 查询条件
     * @return mixed
     */
    protected function findOne($id,$fields = ['*'], $params = []){
        $info = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['author_id']) && $params['author_id']){
                    $query ->where('author_id',$params['author_id']);
                }
                if(isset($params['status']) && $params['status']){
                    $query ->where('status',$params['status']);
                }
            })->with(['cate' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['user' => function ($query) {
                $query->select(['id','truename','avatar','introduce'])->get();
            }])
            ->findOrFail($id);
        $info['cateid'] = $info['catepid'] ? [$info['catepid'],$info['cateid']] : [$info['cateid']];
        if($info['tags']){
            $tags = [];
            foreach($info['tags'] as $item){
                $tags[] = (int)$item;
            }
            $info['tags'] = $tags;
        }
        return $info;
    }


    /**
     * 相关资讯
     * @param $id 排除的主键ID
     * @return mixed
     */
    protected function aboutNews($id){
        return $this->select(['id','title','thumb','created_at','view','cateid'])
            ->where('status',self::STATUS_1)
            ->where('id','<>',$id)
            ->orderBy('created_at','desc')
            ->with(['cateChild' => function ($query) {
                $query->select(['id','name'])->get();
            }])
            ->take(6)
            ->get();
    }


    /**
     * 新闻详情用户文章数据
     * @param $userid 用户ID
     * @return mixed
     */
    protected function userNews($userid){
        return $this->select(['id','title'])
            ->where('status',self::STATUS_1)
            ->where('author_id',$userid)
            ->orderBy('created_at','desc')
            ->take(6)
            ->get();
    }

    /**
     * 统计用户文章数
     * @param $userid
     * @return mixed
     */
    protected function countNews($userid){
        return $this->where('status',self::STATUS_1)
            ->where('author_id',$userid)
            ->count();
    }

    /**
     * 统计用户浏览量
     * @param $userid
     * @return mixed
     */
    protected function countViews($userid){
        return $this->where('status',self::STATUS_1)
            ->where('author_id',$userid)
            ->sum('view');
    }

    /**
     * 查询新闻推荐
     * @return mixed
     */
    protected function newPosition(){
        return $this->select('id','title','thumb')
            ->where('position',self::POSITION_2)
            ->where('status',self::STATUS_1)
            ->orderBy('created_at', 'desc')
            ->take(7)
            ->get();
    }

    /**
     * 查询新闻热门
     * @return mixed
     */
    protected function newHot(){
        return $this->select('id','title','thumb','created_at')
            ->where('hot',self::HOT_2)
            ->where('status',self::STATUS_1)
            ->orderBy('created_at', 'desc')
            ->take(4)
            ->get();
    }


}
